//
//  HPTransactionFailViewController.m
//  HPZZTong
//
//  Created by handpay on 14-4-1.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "HPTransactionFailViewController.h"

@interface HPTransactionFailViewController (){
}
@property(nonatomic, retain)NSString* businessType;
@property (retain, nonatomic) IBOutlet UIButton *goBackButton;
- (IBAction)backClick:(id)sender;

@end

@implementation HPTransactionFailViewController

@synthesize bgImageView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    self.title = @"交易详情";
    [super viewDidLoad];
    
    [self.goBackButton setBackgroundImage:[[UIImage imageNamed:@"bluebutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateNormal];
    
    [self.goBackButton setBackgroundImage:[[UIImage imageNamed:@"bluebuttonhover.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateHighlighted];

    
    UIImage* bgImage = [UIImage imageNamed:@"errorbg.png"];
    
    UIImageView* imageView = [[UIImageView alloc]initWithImage:[bgImage stretchableImageWithLeftCapWidth:0 topCapHeight:8]];
    [imageView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    [self.scrollView addSubview:imageView];
    self.bgImageView = imageView;
    [imageView release];
    
    
#if ZZT_FOR_TEST
    NSDictionary* dic = @{kTransType: transTypeTransfer,
                          kAboveData: @[@[@"商户信息", @"商户名品服饰店"],
                                        @[@"交易状态", @"交易成功"],
                                        @[@"交易流水号", @"0234242"],
                                        @[@"交易时间", @"2014-03-04 10:32"],
                                        @[@"消费卡号", @"6226 **** **** 3333"],
                                        @[@"消费金额", @"¥ 200.00"]],
                          kBelowData: @[@[@"失败原因", @"转出卡余额不足转出卡余额不足",@"3"]]};
    [self setContentData:dic];
#else
    [self setContentData:self.screenData];
#endif
    //
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}
- (int)createItemData:(NSArray *)dataArray withTop:(int)top{
    
    if (!dataArray || [dataArray count] == 0) {
        return top;
    }
    
    int y = top;
    int x = 20;
    int vX = x;
    int h;
    
    for (int i = 0; i < [dataArray count]; i++) {
        h = KLINEHEIGHT;
        NSArray* arr = [dataArray objectAtIndex:i];
        
        CGRect labelRect = CGRectMake(x, y, KLABELWIDTH, h);
        NSTextAlignment textAli = NSTextAlignmentLeft;
        UILabel * label = nil;
        NSString* labelText = [arr objectAtIndex:0];
        
        if (![NSString isNilOrEmpty:labelText]) {
            label = [[UILabel alloc]initWithFrame:labelRect];
            label.textAlignment = NSTextAlignmentLeft;
            label.text = [arr objectAtIndex:0];
            label.backgroundColor = [UIColor clearColor];
            label.textColor = kNormalFontColor;
            [self.scrollView addSubview:label];
            [label release];
            
            textAli = NSTextAlignmentRight;
            vX = x + KLINESPACE + KLABELWIDTH;
        }
        
        
        CGRect valueRect = CGRectMake(vX, y, self.scrollView.frame.size.width - x - vX , h);
        
        UILabel * labelValue = [[UILabel alloc]initWithFrame:valueRect];
        labelValue.textAlignment = textAli;
        labelValue.text = [arr objectAtIndex:1];
        labelValue.textColor = kNormalFontColor;
        labelValue.backgroundColor = [UIColor clearColor];
        
        if (arr.count > 2) {
            NSString* flag = [arr objectAtIndex:2];
            //flag=1,红色  flag 2多行 flag3多行＋红色
            if ([flag isEqualToString:@"1"]) {
                labelValue.textColor = kRedFontColor;
            }else if([flag isEqualToString:@"3"] || [flag isEqualToString:@"2"]){//多行
                labelValue.numberOfLines = 0;
                
                valueRect.size.height = 1000;
                labelValue.frame = valueRect;
                
                [labelValue sizeToFit];
                
                int tempH = labelValue.frame.size.height;
                
                if (tempH >= h) {
                    labelValue.textAlignment = NSTextAlignmentLeft;
                    if (label) {
                        CGRect labelFrame = label.frame;
                        labelFrame.size.height = h;
                        label.frame = labelFrame;
                    }
                    h = tempH;
                }else{
                    h = KLINEHEIGHT;
                    valueRect.size.height = h;
                    labelValue.frame = valueRect;
                }
                if ([flag isEqualToString:@"3"]) {
                    labelValue.textColor = kRedFontColor;
                }
            }
        }
        
        
        [self.scrollView addSubview:labelValue];
        [labelValue release];
        
        y += h + KLINESPACE;
    }
    y += 10;
    
    return  y;
}
- (void)setContentData:(NSDictionary*)dic{
    self.businessType = [self.screenData objectForKey:kBusinissType];
    
     NSDictionary* tempDic = dic;
    if (!dic || [dic count] == 0) {
        tempDic = self.screenData;
    }
    
    if (!tempDic || [tempDic count] == 0) {
        return;
    }
    
    NSArray* belowData = [tempDic objectForKey:kBelowData];
    NSArray* aboveData = [tempDic objectForKey:kAboveData];
    NSString* transType = [tempDic objectForKey:kTransType];
    
    int top = 15;
    int h = 1000;
    int hSpace = 10;
    int left = 15;
    
    CGRect frame = self.scrollView.frame;
    if (self.businessType) {
        if ([self.businessType isEqualToString:transRecord]) {
            self.goBackButton.hidden = YES;
            frame.size.height -= HEIGHT_VIEW_HEADER;
            frame.size.height += TABBAR_HEIGHT;
            if(iOS7Later){
                frame.origin.y -= HEIGHT_STATUS_BAR;
            }
        }
    }
    CGRect titleFrame = CGRectMake(0, top, frame.size.width, h);
    
    UILabel* titleLabel = [[UILabel alloc]initWithFrame:titleFrame];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = kSecondBigFont;
    titleLabel.textColor = [UIColor blackColor];
    [self.scrollView addSubview:titleLabel];
    
    NSString* status = [tempDic objectForKey:@"status"];
    
    h = 30;
    if (transType && [transType isEqualToString:transTypeTransfer]) {//收款
        
        titleLabel.numberOfLines = 0;
        titleLabel.text = ZZT_CLIENG.accountInfo.shopName;
        [titleLabel sizeToFit];
        if (titleLabel.frame.size.height >= h) {
            h = titleLabel.frame.size.height + 8;
        }
        if (![NSString isNilOrEmpty:status] && [status isEqualToString:@"0"]) {
            UIImageView* failImageV = [[UIImageView alloc]initWithFrame:CGRectMake(100, 160, 157, 157)];
            failImageV.image = [UIImage imageNamed:@"failstamp.png"];
            [self.scrollView addSubview:failImageV];
            [failImageV release];
        }
    }else{
        if (![NSString isNilOrEmpty:status] && [status isEqualToString:@"2"]) {
            titleLabel.text = @"交易处理中";
        }else{
            titleLabel.text = @"支付失败";
            UIImageView* failImageV = [[UIImageView alloc]initWithFrame:CGRectMake(100, 160, 157, 157)];
            failImageV.image = [UIImage imageNamed:@"failstamp.png"];
            [self.scrollView addSubview:failImageV];
            [failImageV release];
        }
    }
    titleFrame.size.height = h;
    titleLabel.frame = titleFrame;
    
    [titleLabel release];
    
    top += h + hSpace;
    
    int w = frame.size.width - 2 * left;
    h = 20;
    //@"*****************"
    UILabel* xingLabel = [[UILabel alloc]initWithFrame:CGRectMake(left, top, w, h)];
    xingLabel.backgroundColor = [UIColor clearColor];
    xingLabel.text = @"***************************";
    xingLabel.textColor = kNormalFontColor;
    xingLabel.font = kBigFont;
    xingLabel.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:xingLabel];
    [xingLabel release];
    
    top += h + hSpace;
    
    h = [self createItemData:aboveData withTop:top];
    
    top = h ;
    left = 25;
    w = frame.size.width - 2 * left;
    h = 1;
    
    UIImageView* lineView = [[UIImageView alloc]initWithFrame:CGRectMake(left, top, w, h)];
    lineView.image = [UIImage imageNamed:@"dividingline.png"];
    [self.scrollView addSubview:lineView];
    [lineView release];
    
    top += h + hSpace;
    
    h = [self createItemData:belowData withTop:top];
    
    top = h + hSpace;
    
    if (HP_Device_iPhone5) {
        frame.size.height += IPHONE5_ADD_HEIGHT;
        CGRect buttonFrame = self.goBackButton.frame;
        buttonFrame.origin.y += IPHONE5_ADD_HEIGHT;
        self.goBackButton.frame = buttonFrame;
    }
    if (!iOS7Later) {
        frame.origin.y -= HEIGHT_STATUS_BAR;
    }
    if (top > frame.size.height) {
        self.scrollView.contentSize = CGSizeMake(frame.size.width, top);
    }else{
        frame.size.height = top;
        self.scrollView.contentSize = frame.size;
        CGRect buttonFrame = self.goBackButton.frame;
        buttonFrame.origin.y = frame.origin.y + frame.size.height + 10;
        self.goBackButton.frame = buttonFrame;
    }
   
    self.scrollView.frame = frame;
    
    
    self.bgImageView.frame = CGRectMake(0, 0, self.scrollView.contentSize.width, self.scrollView.contentSize.height);
}
- (void)hideNaviBar{
    if (self.businessType && [self.businessType isEqualToString:transRecord]) {
        [ZZT_UIENG hideNavigationBar:NO];
    }else{
        [ZZT_UIENG hideNavigationBar:YES];
    }
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self hideNaviBar];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self hideNaviBar];
}

- (void)dealloc {
    [_scrollView release];
    self.bgImageView = nil;
    [_goBackButton release];
    self.businessType = nil;
    [super dealloc];
}
- (void)viewDidUnload {
    [self setScrollView:nil];
    [self setGoBackButton:nil];
    [super viewDidUnload];
}
- (IBAction)backClick:(id)sender {
    [self goBack];
}
@end
